﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFFC.Net.Interface;
using EFFC.Net.Common;

namespace EFFC.Net.WebBuilder.WebPage
{
    public class LoginUserModule:PageModule
    {
        public LoginUserModule()
        {
            SetValue("UserID", "");
            SetValue("UserName", "");
            SetValue("RoleID", "");
            SetValue("GroupID", "");
            SetValue("AgentID", "");
            SetValue("AgentName", "");
            SetValue("Actions", new ActionCollection());
        }

        /// <summary>
        /// 登录者的ID
        /// </summary>
        public string UserID
        {
            get { return GetValue<string>("UserID"); }
            set { SetValue("UserID", value); }
        }
        /// <summary>
        /// 登录者的名称
        /// </summary>
        public string UserName
        {
            get { return GetValue<string>("UserName"); }
            set { SetValue("UserName", value); }
        }
        /// <summary>
        /// 角色ID
        /// </summary>
        public string RoleID
        {
            get { return GetValue<string>("RoleID"); }
            set { SetValue("RoleID", value); }
        }
        /// <summary>
        /// 群组ID
        /// </summary>
        public string GroupID
        {
            get { return GetValue<string>("GroupID"); }
            set { SetValue("GroupID", value); }
        }
        /// <summary>
        /// 被代理人的ID
        /// </summary>
        public string AgentID
        {
            get { return GetValue<string>("AgentID"); }
            set { SetValue("AgentID", value); }
        }
        /// <summary>
        /// 被代理人的名称
        /// </summary>
        public string AgentName
        {
            get { return GetValue<string>("AgentName"); }
            set { SetValue("AgentName", value); }
        }

        public ActionCollection Actions
        {
            get { return GetValue<ActionCollection>("Actions"); }
        }

        /// <summary>
        /// 本权限模型基于FunctionID：页面=1：多。
        /// 每个FunctionID会针对权限进行ActionID的设定，FunctionID：ActionID=1：多
        /// </summary>
        public sealed class ActionCollection
        {
            Dictionary<string, ActionEntity> _d = new Dictionary<string, ActionEntity>();
            /// <summary>
            /// 通过PageUrl获取对应的FunctionID
            /// </summary>
            /// <param name="pageUrl">页面的URL</param>
            /// <returns></returns>
            public string GetFunctionIDByPageUrl(string pageUrl)
            {
                if (_d.ContainsKey(pageUrl))
                {
                    return _d[pageUrl].FunctionID;
                }
                else
                {
                    return "";
                }
            }

            /// <summary>
            /// 通过PageUrl获取对应的ParentFunctionID
            /// </summary>
            /// <param name="pageUrl">页面的URL</param>
            /// <returns></returns>
            public string GetParentFunctionIDByPageUrl(string pageUrl)
            {
                if (_d.ContainsKey(pageUrl))
                {
                    return _d[pageUrl].ParentFunctionID;
                }
                else
                {
                    return "";
                }
            }

            /// <summary>
            /// 通过PageUrl获取对应的被授权的ActionID
            /// </summary>
            /// <param name="pageUrl">页面的URL</param>
            /// <returns>多个ActionID会以逗号连接</returns>
            public string GetActionIDsByPageUrl(string pageUrl)
            {
                if (_d.ContainsKey(pageUrl))
                {
                    string rtn = "";
                    foreach (string s in _d[pageUrl].Actions)
                    {
                        rtn += rtn == "" ? s : "," + s;
                    }
                    return rtn;
                }
                else
                {
                    return "";
                }
            }
            /// <summary>
            /// 根据FunctionID获取对应的ActionID
            /// </summary>
            /// <param name="functionID">功能编号</param>
            /// <returns>多个ActionID会以逗号连接</returns>
            public string GetActionIDsByFunctionID(string functionID)
            {
                string rtn = "";
                foreach (ActionEntity ae in _d.Values)
                {
                    if (ae.FunctionID == functionID)
                    {
                        
                        foreach (string s in ae.Actions)
                        {
                            rtn += rtn == "" ? s : "," + s;
                        }
                        break;
                    }
                }

                return rtn;
            }
            /// <summary>
            /// 新增一个Action设定
            /// </summary>
            /// <param name="functionID">FunctionID</param>
            /// <param name="pageurl">页面Url</param>
            /// <param name="actionID">ActionID</param>
            public void AddAction(string functionID,string parentFunctionId, string pageurl, string actionID)
            {
                if (!_d.ContainsKey(pageurl))
                {
                    ActionEntity ae = new ActionEntity();
                    ae.FunctionID = functionID;
                    ae.PageUrl = pageurl;
                    ae.AddAction(actionID);
                    ae.ParentFunctionID = parentFunctionId;

                    _d.Add(ae.PageUrl, ae);
                }
                else
                {
                    _d[pageurl].FunctionID = functionID;
                    _d[pageurl].AddAction(actionID);
                }
            }
            /// <summary>
            /// 根据FunctionID写入新的actionID
            /// </summary>
            /// <param name="functionID">FunctionID</param>
            /// <param name="actionID">待写入的actionID</param>
            public void SetActionByFunctionID(string functionID, string actionID)
            {
                foreach (ActionEntity ae in _d.Values)
                {
                    if (ae.FunctionID == functionID)
                    {
                        ae.AddAction(actionID);
                    }
                }
            }
            /// <summary>
            /// 根页面写入新的actionID
            /// </summary>
            /// <param name="pageUrl">页面Url</param>
            /// <param name="actionID">待写入的ActionID</param>
            public void SetActionByPageUrl(string pageUrl, string actionID)
            {
                if (_d.ContainsKey(pageUrl))
                {
                    _d[pageUrl].AddAction(actionID);
                }
            }
            /// <summary>
            /// 判断是否含有权限设定
            /// </summary>
            public bool HasActions
            {
                get
                {
                    if (_d.Values.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        sealed class ActionEntity
        {
            private string _functionid = "";
            private string _pageurl = "";
            private List<string> _actions = new List<string>();
            private string _parentfunctionid = "";

            public string FunctionID
            {
                get { return _functionid; }
                set { _functionid = value; }
            }

            public string ParentFunctionID
            {
                get { return _parentfunctionid; }
                set { _parentfunctionid = value; }
            }
            public string PageUrl
            {
                get { return _pageurl; }
                set { _pageurl = value; }
            }
            public List<string> Actions
            {
                get { return _actions; }
            }
            /// <summary>
            /// 新增单个ActionID
            /// </summary>
            /// <param name="actionid"></param>
            public void AddAction(string actionid)
            {
                if (!_actions.Contains(actionid))
                {
                    _actions.Add(actionid);
                }
            }
            /// <summary>
            /// 新增多个ActionID
            /// </summary>
            /// <param name="actionid">多个ActionID，每个之间逗号连接</param>
            public void AddActions(string actionids)
            {
                string[] ss = actionids.Split(',');
                foreach (string s in ss)
                {
                    if (!_actions.Contains(s) && !string.IsNullOrEmpty(s))
                    {
                        _actions.Add(s);
                    }
                }
            }
        }
    }

   
}
